package com.efast.dmom.mgmt.service.excel.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.excel.BarcodeRuleBean;
import com.efast.dmom.mgmt.dao.excel.AssociatedTableExcelFileImportDao;
import com.efast.dmom.mgmt.service.excel.AssociatedTableExcelFileImportService;
import com.efast.dmom.mgmt.utils.ExcelUploadUtil;

import jodd.util.StringUtil;

@Service
public class AssociatedTableExcelFileImportServiceImpl implements AssociatedTableExcelFileImportService {

	@Autowired
	private AssociatedTableExcelFileImportDao associatedTableExcelFileImportDao;

	public  boolean isAllRowEmpty(Row row,Row firstRow) {
		int count = 0;
		//单元格数量
		int rowCount = firstRow.getLastCellNum() - firstRow.getFirstCellNum();
		//判断多少个单元格为空
		for (int c = 0; c < rowCount; c++) {
			Cell cell = row.getCell(c);
			if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK || StringUtil.isEmpty((cell+"").trim())){
				count += 1;
			}
		}
		if (count == rowCount) {
			return true;
		}
 
		return  false;
	}
	@Override
	public void uploadExcelFile(File file) {
		try {
			associatedTableExcelFileImportDao.deleteBarcodeRuleErrorInfo();
			// 文件流
			FileInputStream in = new FileInputStream(file);
			Workbook workbook;
			try {
				workbook = ExcelUploadUtil.getWorkbok(in, file);
				// Sheet的数量
				int sheetCount = workbook.getNumberOfSheets();
				// 设置当前excel中sheet的下标：0开始
				for (int i = 0; i < sheetCount; i++) {
					// 遍历第一个Sheet
					Sheet sheet = workbook.getSheetAt(i);
					// 定义一个存储map的集合
					Map<String, Object> map = new HashMap<String, Object>();
					List<BarcodeRuleBean> BarcodeRuleErrorList = new ArrayList<BarcodeRuleBean>();
//					获取实体类的所有属性
//					Field[] field = barcodeRuleBean.getClass().getDeclaredFields();
					// 为跳过几行数据设置count
					int count = 0;
					for (Row row : sheet) {
						BarcodeRuleBean barcodeRuleBean = new BarcodeRuleBean();
						// 默认时跳过第二行插入数据
						if (count < 2) {
							count++;
							continue;
						}
						if (isAllRowEmpty(row,sheet.getRow(0))) {
							continue;
						}
						for (int j = 0; j < 6; j++) {
							Cell cell = row.getCell(j);
							if (cell == null) {
								continue;
							}
							// 获取对应列号的值
							Object obj = ExcelUploadUtil.getValue(cell);
							if (obj == null || " ".equals(obj)) {
								continue;
							} else {
								map.put("field" + j, obj);
							}
						}
						// 首先判断编码名称与规则描述是否存在
						if(map.containsKey("field0")) {
							barcodeRuleBean.setRuleName(String.valueOf(map.get("field0")));	
						}
						if(map.containsKey("field1")) {
							barcodeRuleBean.setDescription(String.valueOf(map.get("field1")));
						}
						if(map.containsKey("field2")) {
							barcodeRuleBean.setBarcodeLength(Integer.parseInt(String.valueOf(map.get("field2"))));	
						}
						if(map.containsKey("field3")) {
							barcodeRuleBean.setStartPattern(String.valueOf(map.get("field3")));	
						}
						if(map.containsKey("field4")) {
							barcodeRuleBean.setEndPattern(String.valueOf(map.get("field4")));
						}
						if(map.containsKey("field5")) {
							barcodeRuleBean.setContainPattern(String.valueOf(map.get("field5")));
						}
						barcodeRuleBean.setCreateTime(new Date());
						barcodeRuleBean.setCreateUser(SpringWebUtils.getRequestUser());
						barcodeRuleBean.setCompanyCode(SpringWebUtils.getRequestCompany());
						BarcodeRuleBean bean = associatedTableExcelFileImportDao
								.judgeRuleNameIsExit(barcodeRuleBean.getRuleName());
						if (bean != null) {
								if(!bean.getDescription().equals(barcodeRuleBean.getDescription())) {
									BarcodeRuleErrorList.add(barcodeRuleBean);
									continue;
								}
							barcodeRuleBean.setRuleId(bean.getRuleId());
							List<BarcodeRuleBean> barcodeRuleItemBeanList = associatedTableExcelFileImportDao
									.selectbarcodeRuleItem(bean.getRuleId());
							if (barcodeRuleItemBeanList != null && barcodeRuleItemBeanList.size() > 0) {
								boolean flag = true;
								if (StringUtil.isNotBlank(barcodeRuleBean.getStartPattern())) {
									for (BarcodeRuleBean data : barcodeRuleItemBeanList) {
										if (StringUtil.isNotBlank(data.getStartPattern())) {
											flag = false;
											break;
										}
									}
									if (!flag) {
									BarcodeRuleErrorList.add(barcodeRuleBean);
									continue;
									}
								}
								if (StringUtil.isNotBlank(barcodeRuleBean.getEndPattern())) {
									for (BarcodeRuleBean data : barcodeRuleItemBeanList) {
										if (StringUtil.isNotBlank(data.getEndPattern())) {
											flag = false;
											break;
										}
									}
									if (!flag) {
									BarcodeRuleErrorList.add(barcodeRuleBean);
									continue;
									}
								}
								if (StringUtil.isNotBlank(barcodeRuleBean.getContainPattern())) {
									for (BarcodeRuleBean data : barcodeRuleItemBeanList) {
										if (StringUtil.isNotBlank(data.getContainPattern())) {
											flag = false;
											break;
										}
									}
									if (!flag) {
									BarcodeRuleErrorList.add(barcodeRuleBean);
									continue;
									}
								}
								if (flag) {
									associatedTableExcelFileImportDao.insertBarcodeRuleItem(barcodeRuleBean);	
								}
							}
						} else {
							associatedTableExcelFileImportDao.insertBarcodeRule(barcodeRuleBean);
							associatedTableExcelFileImportDao.insertBarcodeRuleItem(barcodeRuleBean);	
						}
						map.clear();
					}
					if(BarcodeRuleErrorList != null && BarcodeRuleErrorList.size()>0) {
					//向源码规则报错表中插入数据
					associatedTableExcelFileImportDao.insertBarcodeRuleErrorInfo(BarcodeRuleErrorList);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
